package com.qin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qin.entity.*;
import com.qin.enums.RetCode;
import com.qin.service.TestCaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("testCaseController")
@RequestMapping("/testcase")
@ResponseBody
public class TestCaseController {

    @Autowired
    private TestCaseService testCaseService;

    @RequestMapping(value="/queryTestCases", consumes="application/json;charset=UTF-8", produces="application/json;charset=UTF-8")
    public String queryTestCases(@RequestBody String body){
//        System.out.println("web层：queryTestCases");
        System.out.println("接口调用：/testcase/queryTestCases");

        //入参封装到实体类
        TestCase testCase = JSONObject.parseObject(body, TestCase.class);
        System.out.println("接口入参：" + body);
        //调用service层
        String retString = testCaseService.queryTestCases(testCase).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }

    @RequestMapping(value="/insertTestCase", produces="text/html;charset=UTF-8")
    public String insertTestCase(@RequestBody String body){
//        System.out.println("web层：insertTestCase");
        System.out.println("接口调用：/testcase/insertTestCase");

        //入参封装到实体类
        TestCase testCase = JSONObject.parseObject(body, TestCase.class);
        System.out.println("接口入参：" + body);
        //入参非空判断
        if((testCase.getFunction_id() == null) || ("".equals(testCase.getFunction_id()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "function_id不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }else if((testCase.getCase_name() == null) || ("".equals(testCase.getCase_name()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "case_name不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }

        //调用service层
        String retString = testCaseService.insertTestCase(testCase).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }

    @RequestMapping(value="/deleteTestCase", produces="text/html;charset=UTF-8")
    public String deleteTestCase(@RequestBody String body){
//        System.out.println("web层：deleteTestCase");
        System.out.println("接口调用：/testcase/deleteTestCase");

        //入参封装到实体类
        TestCase testCase = JSONObject.parseObject(body, TestCase.class);
        System.out.println("接口入参：" + body);
        //入参非空判断
        if((testCase.getTestcase_id() == null) || ("".equals(testCase.getTestcase_id()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "testcase_id不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }
        else if((testCase.getFunction_id() == null) || ("".equals(testCase.getFunction_id()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "function_id不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }

        //调用service层
        String retString = testCaseService.deleteTestCase(testCase).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }

    @RequestMapping(value="/updateTestCase", produces="text/html;charset=UTF-8")
    public String updateTestCase(@RequestBody String body){
//        System.out.println("web层：updateTestCase");
        System.out.println("接口调用：/testcase/updateTestCase");

        //入参封装到实体类
        TestCase testCase = JSONObject.parseObject(body, TestCase.class);
        System.out.println("接口入参：" + body);
        //入参非空判断
        if((testCase.getTestcase_id() == null) || ("".equals(testCase.getTestcase_id()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "testcase_id不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }

        //调用service层
        String retString = testCaseService.updateTestCase(testCase).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }

    @RequestMapping(value="/reorderTestCases", produces="text/html;charset=UTF-8")
    public String reorderTestCases(@RequestBody String body){
//        System.out.println("web层：reorderTestCases");
        System.out.println("接口调用：/testcase/reorderTestCases");

        //入参封装到实体类
        TestCaseOrder testCaseOrder = JSONObject.parseObject(body, TestCaseOrder.class);
        System.out.println("接口入参：" + body);
        //入参非空判断
        if((testCaseOrder.getFunction_id() == null) || ("".equals(testCaseOrder.getFunction_id()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "function_id不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }

        //查询公共函数下案例的最大序号
        int max_number = testCaseService.queryMaxOrderByFunctionId(testCaseOrder.getFunction_id());
        //入参参数判断
        if((testCaseOrder.getOld_order() <= 0) || (testCaseOrder.getOld_order() > max_number)){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "old_order参数错误");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }
        else if((testCaseOrder.getNew_order() <= 0) || (testCaseOrder.getNew_order() > max_number)){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "new_order参数错误");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }

        //调用service层
        String retString = testCaseService.reorderTestCases(testCaseOrder).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }

    @RequestMapping(value="/executeTestCase", consumes="application/json;charset=UTF-8", produces="application/json;charset=UTF-8")
    public String executeTestCase(@RequestBody String body){
//        System.out.println("web层：executeTestCase");
        System.out.println("接口调用：/testcase/executeTestCase");

        //入参封装到实体类
        ExecuteBean executeBean = JSONObject.parseObject(body, ExecuteBean.class);
        System.out.println("接口入参：" + body);
        //入参非空判断
        if((executeBean.getTestcase_id() == null) || ("".equals(executeBean.getTestcase_id()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "testcase_id不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }
        //调用service层
        String retString = testCaseService.executeTestCase(executeBean).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }

    @RequestMapping(value="/queryTestCaseDetail", consumes="application/json;charset=UTF-8", produces="application/json;charset=UTF-8")
    public String queryTestCaseDetail(@RequestBody String body){
//        System.out.println("web层：queryTestCaseDetail");
        System.out.println("接口调用：/testcase/queryTestCaseDetail");

        //入参封装到实体类
        Component component = JSONObject.parseObject(body, Component.class);
        System.out.println("接口入参：" + body);
        //入参非空判断
        if((component.getTestcase_id() == null) || ("".equals(component.getTestcase_id()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "testcase_id不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }
        //调用service层
        String retString = testCaseService.queryTestCaseDetail(component).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }


    @RequestMapping(value="/saveTestCaseDetail", consumes="application/json;charset=UTF-8", produces="application/json;charset=UTF-8")
    public String saveTestCaseDetail(@RequestBody String componentInfo){

//        System.out.println("web层：saveTestCaseDetail");
        System.out.println("接口调用：/testcase/saveTestCaseDetail");

        System.out.println("接口入参：" + componentInfo);
        //入参封装到Map和List
        Map inputMmap = (Map)JSON.parse(componentInfo);
        List<Component> componentList = JSON.parseArray(inputMmap.get("componentInfo").toString(), Component.class);
        System.out.println(componentList);

        //入参非空判断
        if((inputMmap.get("componentInfo") == null) || ("".equals(inputMmap.get("componentInfo")))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "componentInfo不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }
        //入参非空判断，遍历componentList
        for(int i=0;i<componentList.size(); i++){
            if((componentList.get(i).getTestcase_id() == null) || ("".equals(componentList.get(i).getTestcase_id()))){
                Map errMap = new HashMap();
                errMap.put("retCode", RetCode.ERROR.getCode());
                errMap.put("retMsg", RetCode.ERROR.getMsg());
                errMap.put("errorMsg", "testcase_id不能为空");
                String retString = new JSONObject(errMap).toString();

                System.out.println("接口出参：" + retString);
                return retString;
            }
            else if(componentList.get(i).getOrder_number() <= 0){
                Map errMap = new HashMap();
                errMap.put("retCode", RetCode.ERROR.getCode());
                errMap.put("retMsg", RetCode.ERROR.getMsg());
                errMap.put("errorMsg", "组件order_number参数错误");
                String retString = new JSONObject(errMap).toString();

                System.out.println("接口出参：" + retString);
                return retString;
            }
            else if((componentList.get(i).getComponent_name() == null) || ("".equals(componentList.get(i).getComponent_name()))){
                Map errMap = new HashMap();
                errMap.put("retCode", RetCode.ERROR.getCode());
                errMap.put("retMsg", RetCode.ERROR.getMsg());
                errMap.put("errorMsg", "component_name不能为空");
                String retString = new JSONObject(errMap).toString();

                System.out.println("接口出参：" + retString);
                return retString;
            }
            //入参非空判断，遍历subComponentList
            List<SubComponent> subComponentList = componentList.get(i).getSubComponentList();
            if(subComponentList != null){
                for(int j=0; j<subComponentList.size(); j++){
                    if(subComponentList.get(i).getOrder_number() <= 0){
                        Map errMap = new HashMap();
                        errMap.put("retCode", RetCode.ERROR.getCode());
                        errMap.put("retMsg", RetCode.ERROR.getMsg());
                        errMap.put("errorMsg", "子组件order_number参数错误");
                        String retString = new JSONObject(errMap).toString();

                        System.out.println("接口出参：" + retString);
                        return retString;
                    }
                    else if((subComponentList.get(i).getSub_component_name() == null) || ("".equals(subComponentList.get(i).getSub_component_name()))){
                        Map errMap = new HashMap();
                        errMap.put("retCode", RetCode.ERROR.getCode());
                        errMap.put("retMsg", RetCode.ERROR.getMsg());
                        errMap.put("errorMsg", "子组件sub_component_name不能为空");
                        String retString = new JSONObject(errMap).toString();

                        System.out.println("接口出参：" + retString);
                        return retString;
                    }
                    else if(subComponentList.get(i).getPoll_interval() <= 0){
                        Map errMap = new HashMap();
                        errMap.put("retCode", RetCode.ERROR.getCode());
                        errMap.put("retMsg", RetCode.ERROR.getMsg());
                        errMap.put("errorMsg", "子组件poll_interval参数错误");
                        String retString = new JSONObject(errMap).toString();

                        System.out.println("接口出参：" + retString);
                        return retString;
                    }
                    else if(subComponentList.get(i).getPoll_duaration() <= 0){
                        Map errMap = new HashMap();
                        errMap.put("retCode", RetCode.ERROR.getCode());
                        errMap.put("retMsg", RetCode.ERROR.getMsg());
                        errMap.put("errorMsg", "子组件poll_duaration参数错误");
                        String retString = new JSONObject(errMap).toString();

                        System.out.println("接口出参：" + retString);
                        return retString;
                    }
                    else if(subComponentList.get(i).getRandom_digit() <= 0){
                        Map errMap = new HashMap();
                        errMap.put("retCode", RetCode.ERROR.getCode());
                        errMap.put("retMsg", RetCode.ERROR.getMsg());
                        errMap.put("errorMsg", "子组件random_digit参数错误");
                        String retString = new JSONObject(errMap).toString();

                        System.out.println("接口出参：" + retString);
                        return retString;
                    }
                }
            }
        }
        //调用service层
        String retString = testCaseService.saveTestCaseDetail(componentList).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }
}
